<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Job extends CI_Controller {
    
    //var $base;    
    public function __construct()
	{
		parent::__construct();
	}

	public function index($name = FALSE)
	{
        $this->form_validation->set_rules('job_number', 'Job Number', 'required|trim|xss_clean|is_natural');
		$this->form_validation->set_rules('postcode', 'Postcode', 'required|trim|xss_clean');
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('job/index');
		}
		else
		{
            $postcode = $this->input->post('postcode');
            $job_number = $this->input->post('job_number');
            
			//$this->get_job();
			if ($result = $this->job_model->verify_job($job_number, $postcode)==TRUE){
				$this->redirectx($postcode, $job_number);
			}else{
                $this->load->view('job/not_found');
            }
        }
	}
    public function redirectx($postcode, $job_number){
        
        	   
	    //////////////////////////////////////////////
        //put all the data into the session
        //////////////////////////////////////////////
        
        $this->session->set_userdata('job_number', $job_number);
        $this->session->set_userdata('postcode', $postcode);
        $customer_id = $this->customer_model->get_customer_id_using_job_number($job_number);
        $this->session->set_userdata('customer_id', $customer_id);
        $customer_name = $this->customer_model->get_customer_name($customer_id);
        $this->session->set_userdata('customer_name', $customer_name);
        redirect('job/get_job');
    }
	public function get_job()
	{
	   
	    //////////////////////////////////////////////
        //obtain data from session and set the variables:
        //////////////////////////////////////////////
        
        if (!$postcode = $this->session->userdata('postcode')){
            redirect('job');
        }
        if (!$job_number = $this->session->userdata('job_number')){
            redirect('job');
        }
        
        $data['job_number'] = $job_number;
        $data['postcode'] = $postcode;
        
        //////////////////////////////////////////////
        //verify if the job exists? if yes:
        //////////////////////////////////////////////
        
        if ($result = $this->job_model->verify_job($job_number, $postcode)==TRUE)
        {   
            //////////////////////////////////////////////
            //save customer id to the session:
            //////////////////////////////////////////////
            $this->session->set_userdata('idcustomer', $result);
            $idcustomer = $result;
            
            //////////////////////////////////////////////
            //is job opened? if yes:
            //display job view and details:
            //////////////////////////////////////////////
           
            if ($result = $this->job_model->job_open($job_number)){
                
                $data['customer_details'] = $this->customer_model->get_customer_details($idcustomer);
                $data['records'] = $this->job_model->get_updates($job_number);
                $data['customer_name'] = $this->customer_model->get_customer_name($idcustomer);
                $data['customer_address'] = $this->customer_model->get_customer_address_using_customer_id($idcustomer);
                $data['job_info'] = $this->job_model->get_job_data($job_number);
                $this->load->view('job/show',$data);
                
            //////////////////////////////////////////////
            //is job opened? if no:
            //////////////////////////////////////////////
            
            }else{
                $this->load->view('job/closed');
            }
            
        //////////////////////////////////////////////
        //verify if the job exists? if no:
        //////////////////////////////////////////////
        
        }else{
            $this->load->view('job/not_found');
        }
	}
    public function customer_add_comment(){
        $this->form_validation->set_rules('content','Form Content' , 'required');
        $this->form_validation->set_rules('job_number','Job Number','required|is_natural'); 
        $this->form_validation->set_rules('postcode','Postcode', 'required');
        
        if($this->form_validation->run()){
            $content = $this->input->post('content');            
            $job_number = $this->input->post('job_number');
            $postcode = $this->input->post('postcode');
            //echo $job_number;
            if($this->job_model->add_comment($content, $job_number, 5)){
                redirect('job/get_job/'.$job_number.'/'.$postcode);
            }else{
               // $this->index();
            }
            
        }else{
            //$this->load->view('job/show');
            $this->get_job();
        }
    }
    

}

